package com.landeb.shop.huo.frag;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.act.ChargeActivity;
import com.landeb.shop.huo.act.GoodsCollectionActivity;
import com.landeb.shop.huo.act.HistoryActivity;
import com.landeb.shop.huo.act.Html5Activity;
import com.landeb.shop.huo.act.LoginActivity;
import com.landeb.shop.huo.act.MoneyLogActivity;
import com.landeb.shop.huo.act.MyOrderActivity;
import com.landeb.shop.huo.act.ProfileEditActivity;
import com.landeb.shop.huo.act.ScoreLogActivity;
import com.landeb.shop.huo.act.SettingActivity;
import com.landeb.shop.huo.bean.BeanStatusInfo;
import com.landeb.shop.huo.bean.BeanUserData;
import com.landeb.shop.huo.httpRequest.UserConfirmationRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.badgeview.BGABadgeLinearLayout;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by qiling on 2017/7/29.
 */

public class FixedFragMine extends Fragment implements View.OnClickListener {

    private static final String BASE_URL = "baseUrl";
    @BindView(R.id.fixed_frag_mine_ll_tishenghuiyuandengji)
    LinearLayout fixedFragMineLlTishenghuiyuandengji;
    @BindView(R.id.fixed_frag_mine_ll_jifen)
    LinearLayout fixedFragMineLlJifen;
    @BindView(R.id.fixed_frag_mine_ll_gouwuquan)
    LinearLayout fixedFragMineLlGouwuquan;
    @BindView(R.id.fixed_frag_mine_ll_jine)
    LinearLayout fixedFragMineLlJie;
    @BindView(R.id.fixed_frag_mine_ll_dengluzhuce)
    LinearLayout fixedFragMineLlDengluzhuce;
    @BindView(R.id.fixed_frag_mine_btn_lijichongzhi)
    Button fixedFragMineBtnLijichongzhi;
    @BindView(R.id.fixed_frag_mine_ll_meiriqiandao)
    LinearLayout fixedFragMineLlMeiriqiandao;
    @BindView(R.id.fixed_frag_mine_badge_daifukuan)
    BGABadgeLinearLayout fixedFragMineBadgeDaifukuan;
    @BindView(R.id.fixed_frag_mine_badge_daishouhuo)
    BGABadgeLinearLayout fixedFragMineBadgeDaishouhuo;
    @BindView(R.id.fixed_frag_mine_badge_daipinglun)
    BGABadgeLinearLayout fixedFragMineBadgeDaipinglun;
    @BindView(R.id.fixed_frag_mine_badge_tuihuanhuo)
    BGABadgeLinearLayout fixedFragMineBadgeTuihuanhuo;
    @BindView(R.id.fixed_frag_mine_badge_quanbudingdan)
    BGABadgeLinearLayout fixedFragMineBadgeQuanbudingdan;
    @BindView(R.id.fixed_frag_mine_ll_wodedizhi)
    LinearLayout fixedFragMineLlWodedizhi;
    @BindView(R.id.fixed_frag_mine_ll_wodeguanzhu)
    LinearLayout fixedFragMineLlWodeguanzhu;
    @BindView(R.id.fixed_frag_mine_ll_kehufuwu)
    LinearLayout fixedFragMineLlKehufuwu;
    @BindView(R.id.fixed_frag_mine_ll_wodezuji)
    LinearLayout fixedFragMineLlWodezuji;
    @BindView(R.id.fixed_frag_mine_ll_xianchangmaidan)
    LinearLayout fixedFragMineLlXianchangmaidan;
    @BindView(R.id.fixed_frag_mine_ll_jihuovip)
    LinearLayout fixedFragMineLlJihuovip;
    @BindView(R.id.fixed_frag_mine_ll_jiarutuiguangren)
    LinearLayout fixedFragMineLlJiarutuiguangren;
    @BindView(R.id.fixed_frag_mine_ll_woyaotuiguang)
    LinearLayout fixedFragMineLlWoyaotuiguang;
    @BindView(R.id.fixed_frag_mine_ll_wodetuiguang)
    LinearLayout fixedFragMineLlWodetuiguang;
    @BindView(R.id.fixed_frag_mine_ll_wodeshezhi)
    LinearLayout fixedFragMineLlWodeshezhi;
    @BindView(R.id.fixed_frag_mine_iv_headPic)
    CircleImageView mFixedFragMineIvHeadPic;
    @BindView(R.id.fixed_frag_mine_tv_userName)
    TextView mFixedFragMineTvUserName;
    @BindView(R.id.fixed_frag_mine_iv_VipLogo)
    ImageView mFixedFragMineIvVipLogo;
    @BindView(R.id.fixed_frag_mine_tv_updateLevel)
    TextView mFixedFragMineTvUpdateLevel;
    @BindView(R.id.fixed_frag_mine_tv_num_jine)
    TextView mFixedFragMineTvNumJine;
    @BindView(R.id.fixed_frag_mine_tv_num_jifen)
    TextView mFixedFragMineTvNumJifen;
    @BindView(R.id.fixed_frag_mine_tv_num_gouwuquan)
    TextView mFixedFragMineTvNumGouwuquan;

    private boolean isViewPrepared = false;
    private boolean isLoaded = false;
    private Unbinder mUnbinder;

    private View view;
    private String mBaseUrl;
    private String mUrl;
    private Retrofit mRetrofit;
    private UserConfirmationRequest mRequest;

    public static FixedFragMine newInstance(String baseUrl) {
        FixedFragMine fragment = new FixedFragMine();
        Bundle args = new Bundle();
        args.putString(BASE_URL, baseUrl);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mBaseUrl = getArguments().getString(BASE_URL);
        }

        OkHttpClient client = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(UserConfirmationRequest.class);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.frag_mine_fixed, container, false);

        mUnbinder = ButterKnife.bind(this, view);

//        mBeanFixedFragHome = new Gson().fromJson(GSON, BeanFixedFragHome.class);

        initView();
        initData();

        initEvent();

//        llToReceive.showTextBadge("123");

        isViewPrepared = true;
        if (!isLoaded && getUserVisibleHint()) {
            refresh();
            isLoaded = true;
        }

        return view;
    }

    private void refresh() {

        mRequest.getUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BeanUserData>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BeanUserData beanUserData) {

                        if (beanUserData.getStatus() == 1) {

                            mFixedFragMineTvUserName.setText(beanUserData.getData().getNickname());

                            String headPicUrl = MyApplication.BASE_URL+"Public/user_head/uid/" + beanUserData.getData().getId();
                            Glide.with(getContext()).load(headPicUrl)
//                                    .transform(new GlideCircleTransform(getContext()))
                                    .into(mFixedFragMineIvHeadPic);

//                            Log.e("uid: ", beanUserData.getData().getId());

                            String lvPicUrl=MyApplication.BASE_URL+"Public/user_level/uid/"+beanUserData.getData().getId();

                            Glide.with(getContext()).load(lvPicUrl)
//                                    .transform(new GlideCircleTransform(getContext()))
                                    .into(mFixedFragMineIvVipLogo);


                            MyApplication.mUserInfo = beanUserData.getData();


                            mFixedFragMineTvNumJine.setText(beanUserData.getData().getMoney());
                            mFixedFragMineTvNumJifen.setText(beanUserData.getData().getScore());
                            mFixedFragMineTvNumGouwuquan.setText(beanUserData.getData().getMoney_coupon());
                            // TODO: 2017/8/29 vip 推广人判断

                        } else {
                            Toast.makeText(getActivity(), beanUserData.getInfo(), Toast.LENGTH_SHORT).show();

                            String headPicUrl = MyApplication.BASE_URL+"Public/user_head/uid/"+"0";
                            Glide.with(getContext()).load(headPicUrl)
                                    .into(mFixedFragMineIvHeadPic);

                            String lvPicUrl=MyApplication.BASE_URL+"Public/user_level/uid/"+"0";

                            Glide.with(getContext()).load(lvPicUrl)
                                    .into(mFixedFragMineIvVipLogo);


                        }


                    }
                });

    }

    private void initEvent() {

        fixedFragMineLlDengluzhuce.setOnClickListener(this);
        fixedFragMineLlTishenghuiyuandengji.setOnClickListener(this);
        fixedFragMineLlJie.setOnClickListener(this);
        fixedFragMineLlJifen.setOnClickListener(this);
        fixedFragMineLlGouwuquan.setOnClickListener(this);
        fixedFragMineBtnLijichongzhi.setOnClickListener(this);
        fixedFragMineLlMeiriqiandao.setOnClickListener(this);
        fixedFragMineBadgeDaifukuan.setOnClickListener(this);
        fixedFragMineBadgeDaishouhuo.setOnClickListener(this);
        fixedFragMineBadgeDaipinglun.setOnClickListener(this);
        fixedFragMineBadgeTuihuanhuo.setOnClickListener(this);
        fixedFragMineBadgeQuanbudingdan.setOnClickListener(this);
        fixedFragMineLlWodedizhi.setOnClickListener(this);
        fixedFragMineLlWodeguanzhu.setOnClickListener(this);
        fixedFragMineLlKehufuwu.setOnClickListener(this);
        fixedFragMineLlWodezuji.setOnClickListener(this);
        fixedFragMineLlXianchangmaidan.setOnClickListener(this);
        fixedFragMineLlJihuovip.setOnClickListener(this);
        fixedFragMineLlJiarutuiguangren.setOnClickListener(this);
        fixedFragMineLlWoyaotuiguang.setOnClickListener(this);
        fixedFragMineLlWodetuiguang.setOnClickListener(this);
        fixedFragMineLlWodeshezhi.setOnClickListener(this);

    }

    private void initData() {

    }

    private void initView() {
    }

    @Override
    public void onResume() {
        super.onResume();
//        如果已经加载过在刷新
        if (isLoaded) {
            refresh();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {

            if (isViewPrepared) {
                refresh();
                isLoaded = true;
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        isViewPrepared = false;
        isLoaded = false;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.fixed_frag_mine_ll_dengluzhuce:

                SharedPreferences preferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
                String token = preferences.getString("token", "null");

                if (token.equals("null")) {
                    LoginActivity.toAct(getContext());
                } else {
                    ProfileEditActivity.toAct(getContext());
                }

                break;
            case R.id.fixed_frag_mine_ll_tishenghuiyuandengji:
                break;
            case R.id.fixed_frag_mine_ll_jine:

                MoneyLogActivity.toAct(getActivity());

                break;
            case R.id.fixed_frag_mine_ll_jifen:
                ScoreLogActivity.toAct(getActivity());

                break;
            case R.id.fixed_frag_mine_ll_gouwuquan:
                break;


            case R.id.fixed_frag_mine_btn_lijichongzhi:
                ChargeActivity.toAct(getActivity());

                break;

            case R.id.fixed_frag_mine_ll_meiriqiandao:

                sign();

                break;


            case R.id.fixed_frag_mine_badge_daifukuan:

                MyOrderActivity.toAct(getContext(), MyOrderActivity.DAIFUKUAN);

                break;

            case R.id.fixed_frag_mine_badge_daishouhuo:

                MyOrderActivity.toAct(getContext(), MyOrderActivity.DAISHOUHUO);

                break;

            case R.id.fixed_frag_mine_badge_daipinglun:

                MyOrderActivity.toAct(getContext(), MyOrderActivity.DAIPINGJIA);

                break;

            case R.id.fixed_frag_mine_badge_tuihuanhuo:

                // TODO: 2017/9/27 退换货改为待收货

                MyOrderActivity.toAct(getContext(), MyOrderActivity.DAIFAHUO);


                break;

            case R.id.fixed_frag_mine_badge_quanbudingdan:
                MyOrderActivity.toAct(getContext(), MyOrderActivity.QUANBU);
                break;

            case R.id.fixed_frag_mine_ll_wodedizhi:

//                Html5Activity.toAct(getActivity(), "收货地址", "http://www.landeb.com/Wap/Index/user_address_lists.html");
                Html5Activity.toAct(getActivity(), "收货地址", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=address");


                break;
            case R.id.fixed_frag_mine_ll_wodeguanzhu:
                GoodsCollectionActivity.toAct(getContext());

                break;
            case R.id.fixed_frag_mine_ll_kehufuwu:
                Html5Activity.toAct(getActivity(), "客户服务", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=service");

                break;
            case R.id.fixed_frag_mine_ll_wodezuji:

                HistoryActivity.toAct(getContext());

                break;
            case R.id.fixed_frag_mine_ll_xianchangmaidan:

                Html5Activity.toAct(getActivity(), "激活VIP", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=maidan");


                break;

            case R.id.fixed_frag_mine_ll_jihuovip:
                Html5Activity.toAct(getActivity(), "激活VIP", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=active_vip");

                break;
            case R.id.fixed_frag_mine_ll_jiarutuiguangren:
                Html5Activity.toAct(getActivity(), "加入推广人", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=active_inviter");

                break;
            case R.id.fixed_frag_mine_ll_woyaotuiguang:
                Html5Activity.toAct(getActivity(), "我要推广", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=invite");

                break;
            case R.id.fixed_frag_mine_ll_wodetuiguang:
                Html5Activity.toAct(getActivity(), "我的推广", MyApplication.BASE_DOMAIN + "/Api/Url/go?url=inviter_list");

                break;
            case R.id.fixed_frag_mine_ll_wodeshezhi:

                SettingActivity.toAct(getContext());
                break;

        }

    }

    private void sign() {

        mRequest.sign()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BeanStatusInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BeanStatusInfo beanStatusInfo) {
                        Toast.makeText(getActivity(), beanStatusInfo.getInfo(), Toast.LENGTH_SHORT).show();
                    }
                });

    }
}
